package com.crm.system.service.impl.messageManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.util.CommonUtils;

import com.crm.model.entity.MessageManager.Message;
import com.crm.model.entity.sys.UserAccount;

import com.crm.model.entity.sys.permissionManagement.SysCompany;
import com.crm.model.qo.messageManager.MarkReadQO;
import com.crm.model.qo.messageManager.MessageAddQO;
import com.crm.model.qo.messageManager.MessageQO;
import com.crm.model.qo.messageManager.ReplyQO;
import com.crm.model.vo.messageManager.MessageVO;

import com.crm.model.vo.messageManager.MsgVO;
import com.crm.model.vo.user.UserAccountManagerVO;
import com.crm.service.messageManager.MessageDbService;

import com.crm.service.sys.permissionManagement.SysCompanyDbService;
import com.crm.system.service.messageManager.MessageService;
import com.crm.system.service.permissionManagement.SysCompanyService;
import com.crm.system.service.user.UserAccountService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import java.util.*;


/**
 * bj
 * 信息
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageDbService messageDbService;

    @Autowired
    private UserAccountService userAccountService;

    @Autowired
    private SysCompanyDbService sysCompanyDbService;

    /**
     * 信息
     * @param pageIndex
     * @param pageSize
     * @param messageQO
     * @param sortName
     * @param sortOrder
     * @return
     */
    @Override
    public DhPageInfo<MessageVO> selectMessageList(Integer pageIndex, Integer pageSize, MessageQO messageQO, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        boolean rs = CommonUtils.isAdmin(userAccount.getUserId());

        List<MessageVO> list = new ArrayList<MessageVO>();
        if("0".equals(messageQO.getBs())) {
            if(rs) {
                //超级管理员
                list = messageDbService.selectMessageList(messageQO);
            }else {
                //普通管理员
                messageQO.setAccountId(userAccount.getCompanyId());
                messageQO.setIdent(1);
                list = messageDbService.selectMessageList(messageQO);
            }
        }else {
            //普通用户
            messageQO.setAccountId(userAccount.getAccountId());
            messageQO.setIdent(2);
            list = messageDbService.selectMessageList(messageQO);
        }

        PageInfo<MessageVO> pageInfo = new PageInfo<MessageVO>(list);
        return new DhPageInfo<MessageVO>(pageInfo, list);
    }

    /**
     * 标记操作(已读和删除)
     * @param qo
     * @return
     */
    @Override
    public Boolean markRead(MarkReadQO qo) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        for(String messageId : qo.getMessageIds()) {
            Message message = new Message();
            message.setMessageId(messageId);
            if("1".equals(qo.getType())) {
                message.setReceiverNewsReadType(0);
            }
            if("2".equals(qo.getType())) {
                //1已发消息  2为已读,未读,审批,回款,系统
                if("1".equals(qo.getMsgType())) {
                    message.setSendDeleteStatus(1);
                }else{
                    message.setReceiverDeleteStatus(1);
                }
            }
            message.setUpdatePeopleId(userAccount.getAccountId());
            message.setUpdateTm(new Date());
            messageDbService.updateMessage(message);
        }

        return true;
    }

    /**
     * 回复
     * @param message
     * @return
     */
    @Override
    public Boolean replyMessage(ReplyQO message) {
        UserAccount user = userAccountService.selectUserAccountByAccountId(message.getSenderId());
        UserAccount us = userAccountService.selectUserAccountByAccountId(message.getReceiverId());
        Message me = new Message();
        List<Message> list = messageDbService.selectBySenderIdAndReceiverId(message.getThemeId());
        if(list.size() == 0) {
            me.setpMessageId(CommonUtils.getGUID());
        }else {
            if(list != null && list.size() > 0){
                Message msg = list.get(0);
                me.setpMessageId(msg.getpMessageId());
            }
        }

        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        me.setMessageId(CommonUtils.getGUID());
        me.setSenderId(userAccount.getAccountId());
        //me.setSenderId(message.getReceiverId());
        me.setSendTm(new Date());
        me.setSendCompanyId(userAccount.getCompanyId());
        me.setThemeId(message.getThemeId());
        me.setTheme(message.getTheme());
        me.setNewsDetail(message.getNewsDetail());
        me.setNewsType(message.getNewsType());
        //1 已发 2别的地方
        if(1 == message.getMsgType()){
            me.setReceiverId(message.getReceiverId());
            me.setReceiverTm(new Date());
            me.setReceiverCompanyId(us.getCompanyId());
        }else{
            me.setReceiverId(message.getSenderId());
            me.setReceiverTm(new Date());
            me.setReceiverCompanyId(user.getCompanyId());
        }
        me.setNewsType(5);
        me.setCreatePeopleId(userAccount.getAccountId());
        me.setCreateTm(new Date());
        me.setUpdatePeopleId(userAccount.getAccountId());
        me.setUpdateTm(new Date());

        return messageDbService.addMessage(me);
    }

    /**
     * 消息详情
     * @param messageId 消息id
     * @param msgType 1已发消息  2为已读,未读,审批,回款,系统
     * @return
     */
    @Override
    public Map<String,Object> messageDetail(String messageId,String msgType) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Map<String,Object> map = new HashMap<>();

        MsgVO message = messageDbService.selectById(messageId);
        if("2".equals(msgType)) {
            List<Message> messages = messageDbService.selectThemeId(message.getThemeId());
            if(messages != null && messages.size() > 0){
                for(Message mm : messages) {
                    if(1 == mm.getReceiverNewsReadType() && userAccount.getAccountId().equals(mm.getReceiverId())) {
                        Message msg = new Message();
                        msg.setMessageId(mm.getMessageId());
                        msg.setReceiverNewsReadType(0);
                        messageDbService.updateMessage(msg);
                    }
                }
            }
        }

        List<MsgVO> nList = new ArrayList<>();
        List<MsgVO> list = messageDbService.selectByThemeId(message.getThemeId());
        if(list != null && list.size() > 0){
            for(MsgVO msg : list) {
                if(userAccount.getAccountId().equals(msg.getSenderId()) && 0 == msg.getSendDeleteStatus()) {
                    nList.add(msg);
                }
                if(userAccount.getAccountId().equals(msg.getReceiverId()) && 0 == msg.getReceiverDeleteStatus()) {
                    nList.add(msg);
                }

            }
        }
        map.put("message",message);
        map.put("list",nList);
        return map;
    }

    /**
     * 删除消息
     * @param themeId 主题id
     * @return
     */
    @Override
    public Boolean deleteThemeId(String themeId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        List<Message> list = messageDbService.selectThemeId(themeId);
        if(list != null && list.size() > 0){
            for(Message msg : list) {
                if(userAccount.getAccountId().equals(msg.getSenderId())) {
                    msg.setSendDeleteStatus(1);
                }
                if(userAccount.getAccountId().equals(msg.getReceiverId())) {
                    msg.setReceiverNewsReadType(0);
                    msg.setReceiverDeleteStatus(1);
                }
                if(1 == msg.getSendDeleteStatus() && 1 == msg.getReceiverDeleteStatus()) {
                    msg.setDeleteStatus(1);
                }

                msg.setUpdatePeopleId(userAccount.getAccountId());
                msg.setUpdateTm(new Date());
                messageDbService.updateMessage(msg);
            }

        }
        return true;
    }

    /**
     * 标记为重要
     * @param messageId 消息id
     * @return
     */
    @Override
    public Boolean markImportant(String messageId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Message message = new Message();
        message.setMessageId(messageId);
        message.setMarkImportant(1);
        message.setUpdatePeopleId(userAccount.getAccountId());
        message.setUpdateTm(new Date());
        Boolean rs = messageDbService.updateMessage(message);
        return rs;
    }

    /**
     * 新增消息
     * @param message
     * @return
     */
    @Override
    public Boolean addMessage(MessageAddQO message) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Message me = new Message();
        me.setMessageId(CommonUtils.getGUID());
        if(!StringUtils.isBlank(message.getBs())) {
            me.setSenderId("xiaozong");
            me.setSendCompanyId("xiaozong");
            me.setCreatePeopleId("xiaozong");
            me.setUpdatePeopleId("xiaozong");
        }else{
            me.setSenderId(userAccount.getAccountId());
            me.setSendCompanyId(userAccount.getCompanyId());
            me.setCreatePeopleId(userAccount.getAccountId());
            me.setUpdatePeopleId(userAccount.getAccountId());
        }
        me.setSendTm(new Date());
        me.setThemeId(CommonUtils.getGUID());
        me.setTheme(message.getTheme());
        me.setNewsDetail(message.getNewsDetail());
        me.setNewsType(message.getNewsType());
        me.setReceiverId(message.getAccountId());
        me.setReceiverTm(new Date());
        me.setReceiverCompanyId(message.getCompanyId());
        me.setCreateTm(new Date());
        me.setUpdateTm(new Date());
        return messageDbService.addMessage(me);
    }

    /**
     * 公司列表
     * @return
     */
    @Override
    public List<SysCompany> selectCompanyList() {
        return sysCompanyDbService.selectSysCompanyList();
    }

    /**
     * 查询用户
     * @return
     */
    @Override
    public List<UserAccountManagerVO> selectUserList(String companyId) {
        return userAccountService.selectUserList(companyId);
    }

    /**
     * 删除单条消息
     * @param messageId 消息id
     * @return
     */
    @Override
    public Boolean deleteByMessageId(String messageId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Message msg = messageDbService.selectByMessageId(messageId);
        if(userAccount.getAccountId().equals(msg.getSenderId())) {
            msg.setSendDeleteStatus(1);

        }
        if(userAccount.getAccountId().equals(msg.getReceiverId())) {
            msg.setReceiverNewsReadType(0);
            msg.setReceiverDeleteStatus(1);
        }
        if(1 == msg.getSendDeleteStatus() && 1 == msg.getReceiverDeleteStatus()) {
            msg.setDeleteStatus(1);
        }
        msg.setUpdatePeopleId(userAccount.getAccountId());
        msg.setUpdateTm(new Date());
        Boolean rs = messageDbService.updateMessage(msg);
        return rs;
    }


}
